package Gui;

import Entity.Account;
import Entity.AccountDetails;
import Entity.DateChooser;
import Entity.Duration;
import Entity.DurationDetails;
import Manager.Manager;
import java.awt.Frame;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;

/**
 *
 * @author CONG
 */
public class DialogRegisterCustomer extends javax.swing.JDialog {

    /** Creates new form DialogRegisterCustomer */
    private Date birthday = null;
    private SystemManager sm;
    private Manager manager;

    public DialogRegisterCustomer(SystemManager parent, boolean modal, Manager manager) {
        super(parent, modal);
        this.sm = parent;
        this.manager = manager;
        initComponents();
        for (int i = 0; i < manager.getBranchManager().getSize(); i++) {
            cbbBranch.addItem(manager.getBranchManager().getBranch(i).getName());
        }
        for (int i = 0; i < manager.getLoanTypeManager().getSize(); i++) {
            cbbLoanType.addItem(manager.getLoanTypeManager().getLoanType(i).getName());
        }
        
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtName = new javax.swing.JTextField();
        txtBirthday = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtAdress = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtPhone = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtOrg = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtSalary = new javax.swing.JTextField();
        txtLoanAmount = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        btnSave = new javax.swing.JButton();
        btnSetDate = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();
        cbbPeriod = new javax.swing.JComboBox();
        cbbBranch = new javax.swing.JComboBox();
        cbbLoanType = new javax.swing.JComboBox();
        jLabel13 = new javax.swing.JLabel();
        lblPercent = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Registeration");

        jPanel1.setPreferredSize(new java.awt.Dimension(563, 64));

        jLabel1.setFont(new java.awt.Font("Arial", 1, 24));
        jLabel1.setForeground(new java.awt.Color(51, 153, 255));
        jLabel1.setText("Loan Registration");
        jPanel1.add(jLabel1);

        jLabel2.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel2.setForeground(new java.awt.Color(44, 44, 44));
        jLabel2.setText("Name:");

        txtName.setPreferredSize(new java.awt.Dimension(59, 22));

        txtBirthday.setEditable(false);
        txtBirthday.setPreferredSize(new java.awt.Dimension(59, 22));

        jLabel3.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel3.setForeground(new java.awt.Color(44, 44, 44));
        jLabel3.setText("Birthday:");

        txtAdress.setPreferredSize(new java.awt.Dimension(59, 22));

        jLabel4.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel4.setForeground(new java.awt.Color(44, 44, 44));
        jLabel4.setText("Address:");

        jLabel5.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel5.setForeground(new java.awt.Color(44, 44, 44));
        jLabel5.setText("Phone:");

        txtPhone.setPreferredSize(new java.awt.Dimension(59, 22));
        txtPhone.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtPhoneKeyTyped(evt);
            }
        });

        jLabel6.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel6.setForeground(new java.awt.Color(44, 44, 44));
        jLabel6.setText("Email:");

        txtEmail.setPreferredSize(new java.awt.Dimension(59, 22));

        jLabel7.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel7.setForeground(new java.awt.Color(44, 44, 44));
        jLabel7.setText("Organization:");

        txtOrg.setPreferredSize(new java.awt.Dimension(59, 22));

        jLabel8.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel8.setForeground(new java.awt.Color(44, 44, 44));
        jLabel8.setText("Salary:");

        txtSalary.setPreferredSize(new java.awt.Dimension(59, 22));
        txtSalary.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtSalaryKeyTyped(evt);
            }
        });

        txtLoanAmount.setPreferredSize(new java.awt.Dimension(59, 22));
        txtLoanAmount.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtLoanAmountKeyTyped(evt);
            }
        });

        jLabel9.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel9.setForeground(new java.awt.Color(44, 44, 44));
        jLabel9.setText("Loan Amount:");

        jLabel10.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel10.setForeground(new java.awt.Color(44, 44, 44));
        jLabel10.setText("Loan Type:");

        jLabel11.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel11.setForeground(new java.awt.Color(44, 44, 44));
        jLabel11.setText("Period Year:");

        jLabel12.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel12.setForeground(new java.awt.Color(44, 44, 44));
        jLabel12.setText("Branch:");

        btnSave.setFont(new java.awt.Font("Arial", 1, 12));
        btnSave.setForeground(new java.awt.Color(44, 44, 44));
        btnSave.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/accept-16x16.png"))); // NOI18N
        btnSave.setText("Submit");
        btnSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSaveActionPerformed(evt);
            }
        });

        btnSetDate.setFont(new java.awt.Font("Arial", 1, 12));
        btnSetDate.setForeground(new java.awt.Color(44, 44, 44));
        btnSetDate.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/calendar-date-16x16.png"))); // NOI18N
        btnSetDate.setText("Set Date");
        btnSetDate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSetDateActionPerformed(evt);
            }
        });

        btnReset.setFont(new java.awt.Font("Arial", 1, 12));
        btnReset.setForeground(new java.awt.Color(44, 44, 44));
        btnReset.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Button-Refresh-16x16.png"))); // NOI18N
        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        btnCancel.setFont(new java.awt.Font("Arial", 1, 12));
        btnCancel.setForeground(new java.awt.Color(44, 44, 44));
        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Cancel-16x16.png"))); // NOI18N
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        cbbPeriod.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1", "2", "3", "4" }));

        cbbLoanType.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbbLoanTypeItemStateChanged(evt);
            }
        });

        jLabel13.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel13.setForeground(new java.awt.Color(44, 44, 44));
        jLabel13.setText("Interest Rate:");

        lblPercent.setFont(new java.awt.Font("Arial", 1, 12));
        lblPercent.setForeground(new java.awt.Color(44, 44, 44));
        lblPercent.setPreferredSize(new java.awt.Dimension(76, 18));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 468, Short.MAX_VALUE)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 257, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(btnSave, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(btnReset, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnCancel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addGap(38, 38, 38))
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addContainerGap(389, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel11)
                    .addComponent(jLabel12)
                    .addComponent(jLabel9)
                    .addComponent(jLabel7)
                    .addComponent(jLabel8)
                    .addComponent(jLabel10))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtSalary, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtOrg, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtAdress, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(txtBirthday, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnSetDate))
                            .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(jLabel13)
                                    .addComponent(txtLoanAmount, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(lblPercent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(cbbLoanType, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbbBranch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbbPeriod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(79, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(txtBirthday, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSetDate))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(btnSave)
                    .addComponent(txtAdress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(btnReset)
                    .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(btnCancel)
                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtOrg, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(txtSalary, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(txtLoanAmount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(cbbLoanType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel13)
                    .addComponent(lblPercent, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(cbbPeriod, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(cbbBranch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * Exit dialog
     * @param evt 
     */
private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
// TODO add your handling code here:
    this.dispose();
}//GEN-LAST:event_btnCancelActionPerformed
    /**
     * Reset Dialog
     * @param evt 
     */
private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
// TODO add your handling code here:
    txtAdress.setText("");
    txtBirthday.setText("");
    txtEmail.setText("");
    txtLoanAmount.setText("");
    txtName.setText("");
    txtOrg.setText("");
    txtSalary.setText("");
    cbbBranch.setSelectedIndex(0);
    cbbLoanType.setSelectedIndex(0);
    cbbPeriod.setSelectedIndex(0);
    txtPhone.setText("");
}//GEN-LAST:event_btnResetActionPerformed

private void btnSetDateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSetDateActionPerformed
// TODO add your handling code here:
    DateChooser dateChooser = new DateChooser((Frame) null, "Select your birthday");
    Date getDate = new Date();
    getDate = dateChooser.select(getDate);
    if (getDate == null) {
        birthday = new Date();
        return;
    }
    birthday = getDate;
    SimpleDateFormat formatDate = new SimpleDateFormat("MM/dd/yyyy");
    txtBirthday.setText(formatDate.format(birthday));
}//GEN-LAST:event_btnSetDateActionPerformed
    private String name;
    private String phone;
    private String address;
    private String email;
    private String organization;
    private float salary;
    private float amountLoan;
private void btnSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSaveActionPerformed
// TODO add your handling code here:
    email = txtEmail.getText().trim();
    if (sm.checkEmailExist(email)) {
        JOptionPane.showConfirmDialog(this, "Email existed. Please choice another email address.", "Error", JOptionPane.ERROR_MESSAGE);
        return;
    } else if (checkValid()) {
        int loanTypeCode = -1;
        int rate = 0;
        float amountDue = 0;
        String loanType = (String) cbbLoanType.getSelectedItem();
        for (int i = 0; i < manager.getLoanTypeManager().getSize(); i++) {
            if (manager.getLoanTypeManager().getLoanType(i).getName() == null ? loanType == null : manager.getLoanTypeManager().getLoanType(i).getName().equals(loanType)) {
                loanTypeCode = manager.getLoanTypeManager().getLoanType(i).getLoanTypeCode();
                rate = (int) manager.getLoanTypeManager().getLoanType(i).getInterestRate();
            }
        }
        
        String str = (String) cbbPeriod.getSelectedItem();
        int period = Integer.parseInt(str);
        //Date startDate = new Date();
        Calendar calendar = Calendar.getInstance();
        Calendar calendar2 = Calendar.getInstance();
        Date startDate = calendar2.getTime();
        Date endDate = null;
        switch (period) {
            case 1: {
                calendar.add(Calendar.YEAR, 1);
                endDate = calendar.getTime();
                amountDue = (amountLoan * (100 + rate) / 100) / 12;
                break;
            }
            case 2: {
                calendar.add(Calendar.YEAR, 2);
                endDate = calendar.getTime();
                amountDue = (amountLoan * (100 + rate) / 100) / 24;
                break;
            }
            case 3: {
                calendar.add(Calendar.YEAR, 3);
                endDate = calendar.getTime();
                amountDue = (amountLoan * (100 + rate) / 100) / 36;
                break;
            }
            case 4: {
                calendar.add(Calendar.YEAR, 4);
                endDate = calendar.getTime();
                amountDue = (amountLoan * (100 + rate) / 100) / 48;
                break;
            }
        }
        String branchName = (String) cbbBranch.getSelectedItem();
        float balance = amountLoan * (100 + rate) / 100;
        Duration dur = new Duration(loanTypeCode, startDate, amountLoan, endDate, balance, true, true);
        Duration dur2 = sm.addDuration(dur);
        int accountLoanCode = dur2.getAccountLoanCode();
        Account acc = new Account(birthday, name, phone, address, email, organization, salary);
        sm.addAccount(acc, accountLoanCode, period, loanTypeCode);
        AccountDetails accde = new AccountDetails(acc.getResgisterNo(), accountLoanCode, loanTypeCode, period);
        sm.addAccountDetails(accde);
        DurationDetails durde = null;
        SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
        for (int i = 0; i < period * 12; i++) {
            calendar2.add(Calendar.MONTH, 1);
            startDate = calendar2.getTime();
            durde = new DurationDetails(accountLoanCode, 1, startDate, amountDue, 0, null, false);
            sm.addDurationHistory(durde);
        }
        JOptionPane.showMessageDialog(this, "You're registration successful.");
        this.dispose();
    }
}//GEN-LAST:event_btnSaveActionPerformed

private void txtSalaryKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtSalaryKeyTyped
// TODO add your handling code here:
    String number="0123456789";
    if(number.indexOf(evt.getKeyChar())==-1){
        evt.consume();
    }
}//GEN-LAST:event_txtSalaryKeyTyped

private void txtLoanAmountKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtLoanAmountKeyTyped
// TODO add your handling code here:
    String number="0123456789";
    if(number.indexOf(evt.getKeyChar())==-1){
        evt.consume();
    }
}//GEN-LAST:event_txtLoanAmountKeyTyped

private void cbbLoanTypeItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbbLoanTypeItemStateChanged
// TODO add your handling code here:
    if(cbbLoanType.getSelectedIndex()==0){
        lblPercent.setText("10%");
    }else if(cbbLoanType.getSelectedIndex()==1){
        lblPercent.setText("13%");
    }else if(cbbLoanType.getSelectedIndex()==2){
        lblPercent.setText("18%");
    }else if(cbbLoanType.getSelectedIndex()==3){
        lblPercent.setText("5%");
    }
}//GEN-LAST:event_cbbLoanTypeItemStateChanged

private void txtPhoneKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPhoneKeyTyped
// TODO add your handling code here:
    String number="0123456789";
    if(number.indexOf(evt.getKeyChar())==-1){
        evt.consume();
    }
}//GEN-LAST:event_txtPhoneKeyTyped
    public boolean checkValid() {
        name = txtName.getText().trim();
        phone = txtPhone.getText().trim();
        address = txtAdress.getText().trim();
        email = txtEmail.getText().trim();
        organization = txtOrg.getText().trim();
        Date currentDate = new Date();
        if (name.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Name can not be blank", "Error", JOptionPane.ERROR_MESSAGE);
            txtName.requestFocus();
            return false;
        } else if (currentDate.getYear() - birthday.getYear() < 20) {
            JOptionPane.showConfirmDialog(this, "Age must be larger than 20", "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        } else if (address.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Addres can not be blank", "Error", JOptionPane.ERROR_MESSAGE);
            txtAdress.requestFocus();
            return false;
        } else if (!Pattern.matches("0[\\d]{8,11}", phone) || phone.equals("")) {
            JOptionPane.showMessageDialog(this, "Phone must be 8 - 11 digits and start with 0.", "Error", JOptionPane.ERROR_MESSAGE);
            txtPhone.requestFocus();
            return false;
        } else if (!Pattern.matches(".+@.+\\.[a-z]+", email)) {
            JOptionPane.showMessageDialog(this, "Email must be abc@xyz.com", "Error", JOptionPane.ERROR_MESSAGE);
            txtEmail.requestFocus();
            return false;
        } else if (organization.isEmpty()) {
            JOptionPane.showMessageDialog(this, "Organization can not be blank", "Error", JOptionPane.ERROR_MESSAGE);
            txtOrg.requestFocus();
            return false;
        }
        try {
            salary = Float.parseFloat(txtSalary.getText());
            if (salary == 0 || salary > 999999 || salary < 2000) {
                JOptionPane.showMessageDialog(this, "Salary do not zero,must be larger than 2000", "Error", JOptionPane.ERROR_MESSAGE);
                txtSalary.requestFocus();
                return false;
            }
        } catch (NumberFormatException ex) {
            JOptionPane.showMessageDialog(this, "Salary must be a number.", "Error", JOptionPane.ERROR_MESSAGE);
            txtSalary.requestFocus();
            return false;
        }

        try {
            amountLoan = Float.parseFloat(txtLoanAmount.getText());
            if (amountLoan < 100000) {
                JOptionPane.showConfirmDialog(this, "Loan Amount can not <=100000", "Error", JOptionPane.ERROR_MESSAGE);
                txtLoanAmount.requestFocus();
                return false;
            } else if (amountLoan > 500000) {
                JOptionPane.showConfirmDialog(this, "Loan Amount can not > 500000", "Error", JOptionPane.ERROR_MESSAGE);
                txtLoanAmount.requestFocus();
                return false;
            }
        } catch (NumberFormatException ex) {
            JOptionPane.showConfirmDialog(this, "Loan amount must be a number.", "Error", JOptionPane.ERROR_MESSAGE);
            txtLoanAmount.requestFocus();
            return false;
        }
        return true;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnReset;
    private javax.swing.JButton btnSave;
    private javax.swing.JButton btnSetDate;
    private javax.swing.JComboBox cbbBranch;
    private javax.swing.JComboBox cbbLoanType;
    private javax.swing.JComboBox cbbPeriod;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblPercent;
    private javax.swing.JTextField txtAdress;
    private javax.swing.JTextField txtBirthday;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtLoanAmount;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtOrg;
    private javax.swing.JTextField txtPhone;
    private javax.swing.JTextField txtSalary;
    // End of variables declaration//GEN-END:variables
}
